import logging
import unittest

from requests import Session

from api.register_api import RegisterAPI
from parameterized import parameterized
from utils import common_assert, read_data, clear_member_data


class TestRegisterApi(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        clear_member_data("18500000007")

    def setUp(self):
        self.session = Session()
        self.client = RegisterAPI(self.session)
        logging.info(F"创建session对象地址{self.session}")

    def tearDown(self):
        self.session.close()
        logging.info(F"session对象{self.session}被关闭")

    # 获取图片验证码接口测试
    @parameterized.expand(read_data("register_data.json", "captcha_code_api"))
    def test01_captcha_code_api(self, random, expect_code):
        try:
            resp = self.client.captcha_code_api(random)
            logging.info(f"预期状态码: {expect_code} / 响应状态码: {resp.status_code}")
            self.assertEqual(expect_code, resp.status_code)
            logging.info("断言通过")
        except Exception as e:
            logging.error(f"断言失败: {e}")
            raise

    # 获取手机验证码接口测试
    @parameterized.expand(read_data("register_data.json", "phone_code_api"))
    def test02_phone_code_api(self, random, phone, captcha, expect):
        try:
            self.client.captcha_code_api(random)
            resp = self.client.phone_code_api(phone, captcha)
            logging.info(f"预取结果: {expect} / 响应结果: {resp.text}")
            common_assert(self, resp, expect)
            logging.info("断言通过")
        except Exception as e:
            logging.error(f"断言失败: {e}")
            raise

    # 注册接口测试
    @parameterized.expand(read_data("register_data.json", "register_api"))
    def test03_register_api(self, random, phone, password, captcha, phone_code, invite_phone, expect):
        try:
            self.client.captcha_code_api(random)
            self.client.phone_code_api(phone, captcha)
            resp = self.client.register_api(phone, password, captcha, phone_code, invite_phone)
            logging.info(f"预取结果: {expect} / 响应结果: {resp.text}")
            common_assert(self, resp, expect)
            logging.info("断言通过")
        except Exception as e:
            logging.error(f"断言失败: {e}")
            raise
